/**
 * Created by iallai on 2/1/2018.
 */

import axios from 'axios'
import {CONFIG} from '@/config/env'

let API=CONFIG.PROJECT.API;


const query = (query={page:1,size:5}) => new Promise((resolve, reject) => {


  axios.get(`${API.URL}/bg_users`,{
    params:query
  }).then(function(res){
      if(API.isOK(res.data)){
        resolve(res.data);
      }
    })
    .catch(function(err){
      reject("网络错误");
  });



});

const get = (data)=>new Promise((resolve, reject) => {

  //console.log(data);
  axios({
    method: 'get',
    url: `${API.URL}/bg_users/${data.id}`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });


});
const add = (data)=>new Promise((resolve, reject) => {

  //console.log(data);
  axios({
    method: 'get',
    url: `${API.URL}/bg_users/${data.id}`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });


});
const del = (data)=>new Promise((resolve, reject) => {

  //console.log(data);
  axios({
    method: 'delete',
    url: `${API.URL}/bg_users/${data.id}`,
    data:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });


});


const update= (data)=>new Promise((resolve, reject) => {

  //console.log(data);
  axios({
    method: 'post',
    url: `${API.URL}/bg_users/${data.id}`,
    params:data,

  }).then(function(res){
    if(API.isOK(res.data)){
      resolve(res.data);
    }else{
      reject(res.data.message);
    }
  }).catch(function(err){
    reject("网络错误");
  });


});



export {query,get,del,update,add};

